package sk.sigp.aobot.swinggui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTable;

import sk.sigp.aobot.swinggui.utils.GroupTableModel;
import sk.sigp.aobot.swinggui.utils.StringActionListener;

/**
 * @author mathew
 *
 */
class ButtonTableEditor extends DefaultCellEditor {

	private JButton button = new JButton("o");
	private int selectedRow = -1; 
		
	private GroupTableModel model;
	
	private StringActionListener detachedAction;
	
	private static final long serialVersionUID = 1L;

	@Override
	public Component getTableCellEditorComponent(JTable table,
			Object value, boolean isSelected, int row, int column) {
		selectedRow = row;
		return button;
	}

	public ButtonTableEditor(GroupTableModel model, StringActionListener action) {
		super(new JCheckBox());
		this.model = model;
		this.setDetachedAction(action);
		
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String group = getModel().getData().get(selectedRow).getKey();
				if (getDetachedAction()!=null)
					getDetachedAction().action(group);
//				GroupFrame frame = new GroupFrame(group);
//				x.addDetailedListener(frame);
//				frame.setVisible(true);
			}
		});
		
	}

	public GroupTableModel getModel() {
		return model;
	}

	public void setModel(GroupTableModel model) {
		this.model = model;
	}

	public StringActionListener getDetachedAction() {
		return detachedAction;
	}

	public void setDetachedAction(StringActionListener detachedAction) {
		this.detachedAction = detachedAction;
	}
	
}
